package com.jt.friendsmanagement;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import static android.content.ContentValues.TAG;

public class AddFriendFragment extends Fragment {
    private static final String ARG_FRIEND_ID = "friend_id";
    private static final String[] sHobbies = {"电影", "编程", "篮球", "足球", "游泳", "羽毛球", "绘画", "写作", "美食"};

    private Friend mFriend;
    private EditText mName;
    private RadioGroup mSexRadioGroup;
    private RadioButton mSexMale;
    private RadioButton mSexFemale;
    private EditText mBirthdayEditText;
    private CheckBox[] mCheckBoxes = new CheckBox[10];//第0个不用
    private Button mConfirmButton;
    private Button mCancelButton;


    public static AddFriendFragment newInstance(UUID friendId) {
        Bundle args = new Bundle();
        args.putSerializable(ARG_FRIEND_ID, friendId);

        AddFriendFragment fragment = new AddFriendFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        UUID friendId = (UUID) getArguments().getSerializable(ARG_FRIEND_ID);
        mFriend = FriendLab.get(getActivity()).getFriend(friendId);


    }

    @Nullable
    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_addfriend, container, false);
        mName = v.findViewById(R.id.friend_name);
        mName.setText(mFriend.getName());
        mSexRadioGroup = v.findViewById(R.id.friend_sex);
        mSexMale = v.findViewById(R.id.male);
        mSexFemale = v.findViewById(R.id.female);
        if ("男".equals(mFriend.getSex())) {
            mSexMale.setChecked(true);
        }
        if ("女".equals(mFriend.getSex())) {

            mSexFemale.setChecked(true);
        }
        mBirthdayEditText = v.findViewById(R.id.birthday_edittext);
        mBirthdayEditText.setText(mFriend.getBirthday().toString());
        mBirthdayEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDatePickDlg();
            }
        });
        /*reflection 获取多选框控件，超过三个 Toast*/
        for (int i = 1; i <= 9; i++) {
            String checkbox_string = "checkBox" + i;
            mCheckBoxes[i] = v.findViewById(getResources().getIdentifier(checkbox_string, "id", getContext().getPackageName()));
            mCheckBoxes[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    int checked_count = 0;
                    for (int j = 1; j <= 9; j++) {
                        if (mCheckBoxes[j].isChecked()) {
                            checked_count++;
                        }
                        if (checked_count > 3) {
                            compoundButton.setChecked(false);
                            Toast.makeText(getContext(), "最多选择三个爱好", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }

                }
            });
        }

        for (int i = 1; i < mCheckBoxes.length; i++) {
            String hobbies[] = mFriend.getHobbies();
            for (String hobby : hobbies) {
                if (sHobbies[i - 1].equals(hobby)) {
                    mCheckBoxes[i].setChecked(true);
                }
            }
        }


        mConfirmButton = v.findViewById(R.id.confirm_button);
        mCancelButton = v.findViewById(R.id.cancel_button);
        mConfirmButton.setOnClickListener(new View.OnClickListener() {
            /*添加点击确定按钮功能,生日在onDateSet中已经设置*/
            @Override
            public void onClick(View view) {
                mFriend.setName(mName.getText().toString());
                if (mSexMale.isChecked()) mFriend.setSex("男");
                if (mSexFemale.isChecked()) mFriend.setSex("女");
                /*设置爱好*/
                StringBuilder sb = new StringBuilder();
                for (int i = 1; i < mCheckBoxes.length; i++) {
                    if (mCheckBoxes[i].isChecked()) {
                        sb.append(sHobbies[i - 1] + " ");
                    }
                }
                String hobbies[] = sb.toString().split("\\s");
                mFriend.setHobbies(hobbies);
                getActivity().finish();
            }
        });
        mCancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FriendLab friendLab = FriendLab.get(getActivity());
                friendLab.getFriends().remove(mFriend);
                getActivity().finish();
            }
        });
        return v;
    }

    private void showDatePickDlg() {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(mFriend.getBirthday());
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                Calendar calendar1 = Calendar.getInstance();
                calendar1.set(i, i1, i2);
                mFriend.setBirthday(calendar1.getTime());
                String date = (String) DateFormat.format("yyyy年MM月dd日", mFriend.getBirthday());
                mBirthdayEditText.setText(date);
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }
}
